<template>
  <div v-if="widget.type === 'grid-col'" class="grid-cell"
       :class="[selected ? 'selected' : '', customClass]"
       @click.stop="selectWidget(widget)">

    <draggable :list="widget.widgetList" item-key="id" v-bind="{group:'dragGroup', ghostClass: 'ghost',animation: 400}"
               tag="div"
               :component-data="{name: 'fade', class: 'drag-drop-zone'}"
               :style="[widget.options.containerStyle]"
               handle=".drag-handler" @end="(evt) => onGridDragEnd(evt, widget.widgetList)"
               @add="(evt) => onGridDragAdd(evt, widget.widgetList)"
               @update="onGridDragUpdate"
               :move="checkContainerMove">
      <template #item="{ element: subWidget, index: swIdx }">
        <div class="form-widget-list">
          <template v-if="'container' === subWidget.category">
            <component :is="subWidget.type + '-widget'" :widget="subWidget" :designer="designer" :key="subWidget.id"
                       :parent-list="widget.widgetList"
                       :index-of-parent-list="swIdx" :parent-widget="widget"></component>
          </template>
          <template v-else>
            <component :is="subWidget.type + '-widget'" :field="subWidget" :designer="designer" :key="subWidget.id"
                       :parent-list="widget.widgetList"
                       :index-of-parent-list="swIdx" :parent-widget="widget" :design-state="true"></component>
          </template>
        </div>
      </template>
    </draggable>
    <div class="grid-col-action" v-if="designer.selectedId === widget.id && widget.type === 'grid-col'">
      <i :title="i18nt('designer.hint.remove')" @click.stop="removeWidget">
        <svg-icon icon-class="el-delete" />
      </i>
    </div>
  </div>
</template>

<script>
import i18n from '@/utils/i18n'
import FieldComponents from '@/components/form-designer/form-widget/field-widget/index'
import SvgIcon from '@/components/svg-icon'
import refMixinDesign from '@/components/form-designer/refMixinDesign'
import Draggable from 'vuedraggable'

export default {
  name: 'GridColWidget',
  componentName: 'GridColWidget',
  mixins: [i18n, refMixinDesign],
  components: {
    Draggable,
    ...FieldComponents,
    SvgIcon,
  },
  props: {
    widget: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,

    colHeight: {
      type: String,
      default: null,
    },

  },
  computed: {
    selected() {
      return this.widget.id === this.designer.selectedId
    },

    customClass() {
      return this.widget.options.customClass || ''
    },

    /*colHeightStyle() {
      return !!this.colHeight ? { height: this.colHeight + 'px' } : {}
    },*/
  },
  created() {
    // this.initRefList()
    // this.initLayoutProps()
  },
  methods: {
    removeWidget() {
      this.$emit('remove-widget')
    },
    onGridDragEnd(evt, subList) {
      //
    },

    onGridDragAdd(evt, subList) {
      console.log('onGridDragAdd')
      const newIndex = evt.newIndex
      if (!!subList[newIndex]) {
        this.designer.setSelected(subList[newIndex])
      }

      this.designer.emitHistoryChange()
      this.designer.emitEvent('field-selected', this.widget)
    },

    onGridDragUpdate() {
      this.designer.emitHistoryChange()
    },

    selectWidget(widget) {
      console.log('id: ' + widget.id)
      this.designer.setSelected(widget)
    },

    checkContainerMove(evt) {
      return this.designer.checkWidgetMove(evt)
    },

  },
}
</script>

<style lang="scss" scoped>

/*.grid-stack {
  background: rgba(249 250 251);
}
:deep(.grid-stack-item-content) {
  background-color: white;
  outline: 1px solid #cccccc;
}*/
.grid-cell {
  box-sizing: border-box;
  min-height: 36px !important;
  position: relative;
  height: 100%;

  .form-widget-list {
    min-height: 28px;
    display: inline;
    width: 100%;
    broder: 1px solid #336699;
  }

  .drag-drop-zone {
    height: 100%;

    /* &:hover {
       background: rgba(74, 255, 226, 0.44);
     }*/
  }

  .grid-col-action {
    position: absolute;
    bottom: 0;
    right: -2px;
    height: 28px;
    line-height: 28px;
    background: var(--ep-color-primary);
    z-index: 999;

    i {
      font-size: 14px;
      color: #fff;
      margin: 0 5px;
      cursor: pointer;
    }
  }

  .grid-col-handler {
    position: absolute;
    top: -2px;
    left: -2px;
    height: 22px;
    line-height: 22px;
    background: var(--ep-color-primary);
    z-index: 9;

    i {
      font-size: 14px;
      font-style: normal;
      color: #fff;
      margin: 4px;
      cursor: default;
    }
  }
}

.grid-col-action, .grid-col-handler {
  :deep(.svg-icon) {
    margin-left: 0.1em;
    margin-right: 0.1em;
  }
}

</style>
